#
#   Hello World server in Python
#   Binds REP socket to tcp://*:5555
#   Expects "Hello" from client, replies with "World"
#
import zmq
import time
import uuid

import middleman

context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind("tcp://*:5556")

raw_input("press any key to start server")

while True:
    #  Wait for next request from client
    message = socket.recv()

    print "Received request: ", message

    request = middleman.NewGameRequest("game", 1)
    request.deserialize(message)

    print "Request: ", request.serialize()
    
    game = middleman.Game(str(uuid.uuid4()), request.get_game_name(), request.get_max_player_count())

    print "Game: ", game
    
    response = middleman.GameCreatedResponse(game)

    #  Do some 'work'
    time.sleep (0.5)        #   Do some 'work'

    print "Response: ", response

    #  Send reply back to client
    socket.send(response.serialize())


raw_input("server has stopped. press any key to exit.")